<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Eye View Design CMS module Ajax Model
 *
 * PHP version 5
 *
 * @category  CodeIgniter
 * @package   EVD CMS
 * @author    Frederico Carvalho
 * @copyright 2008 Mentes 100Limites
 * @version   0.1
 */

class Ajax_model extends CI_Model
{
	/**
	 * Instanciar o CI
	 */
	public function __construct()
	{
		parent::__construct();
		//$this->CI =& get_instance();
	}

	public function get_countries()
	{
		$this->db->select('vcode,vfirstname,vlastname,vtype,vmobile');
		$this->db->from('vendor');
		$this->db->join('vendor_code', 'vendor_code.uuid = vendor.uuid');

		$this->flexigrid->build_query();

		//Select table name
		//$table_name = "country";

		//Build contents query
		//$this->db->select('id,iso,name,printable_name,iso3,numcode')->from($table_name);
		//$this->CI->flexigrid->build_query();

		//Get contents
		$return['records'] = $this->db->get();

		//Build count query
		$this->db->select('count(vendor.uuid) as record_count');
		$this->db->from('vendor');
		$this->db->join('vendor_code', 'vendor_code.uuid = vendor.uuid');
		$this->flexigrid->build_query(FALSE,FALSE);
		$record_count = $this->db->get();
		$row = $record_count->row();

		//Get Record Count
		$return['record_count'] = $row->record_count;

		//Return all
		return $return;
	}

	/**
	 * Remove country
	 * @param int country id
	 * @return boolean
	 */
	public function delete_country($country_id)
	{
		$delete_country = $this->db->query('DELETE FROM country WHERE id='.$country_id);

		return TRUE;
	}

	public function get_ajax_vendor(){
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];

		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			$typequery = explode(',', $_POST['qtype']);
			$query = explode(',',$_POST['query']);
			$where = "WHERE 1";
			for ($i=0;$i<4;$i++){
				if ($query[$i]){
					$where .= " AND ".$typequery[$i]." LIKE '%".$query[$i]."%'";
				}
			}
		} else {
			$where ='';
		}

		
		if ($sortname == 'uuid'){
			$sortname = 'vendor.'.$sortname;
		}
		$sort = "ORDER BY $sortname $sortorder";

		if (!$page) $page = 1;
		if (!$rp) $rp = 10;

		$start = (($page-1) * $rp);

		$limit = "LIMIT $start, $rp";

		
			
		$sql = "SELECT * FROM vendor 
		JOIN vendor_address ON vendor_address.uuid = vendor.uuid 
		JOIN option_village ON option_village.villageid = vendor_address.village  
			$where $sort $limit
		";

			$return['records'] = $this->db->query($sql);

			$sql = "SELECT count(vendor.uuid) as rcount
		FROM vendor 
		JOIN vendor_address ON vendor_address.uuid = vendor.uuid 
		JOIN option_village ON vendor_address.village = option_village.villageid 
		$where";

		$result = $this->db->query($sql);

		$row = $result->row_array();
		$return['record_count'] = $row['rcount'];

		return $return;
	}
	
public function get_ajax_truck(){
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];

		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			$where = "WHERE ".$typequery." LIKE '%".$_POST['query']."%' ";
		} else {
			$where ='';
		}
		
		$sort = "ORDER BY $sortname $sortorder";

		if (!$page) $page = 1;
		if (!$rp) $rp = 10;

		$start = (($page-1) * $rp);

		$limit = "LIMIT $start, $rp";

		
			
		$sql = "SELECT * FROM truck
			$where $sort $limit
		";

			$return['records'] = $this->db->query($sql);

			$sql = "SELECT count(*) as rcount
		FROM truck
		$where";

		$result = $this->db->query($sql);

		$row = $result->row_array();
		$return['record_count'] = $row['rcount'];

		return $return;
	}
	
}
?>